﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MNNUTEM
{
    public partial class Costos : Form
    {
        private crudCosto cCosto;
        int idProducto = 1,idCosto=1,sw=0, idSucursal, arriendo, cantidadt, bodega,cantidadp;
        public Costos()
        {
            InitializeComponent();
            cCosto = new crudCosto();
        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void updateGridView()
        {
            dataGridView2.DataSource = cCosto.Consultation("select *from Costo");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            cCosto.Insert(textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8);
            updateGridView();
            Inventario inv = new Inventario();
            inv.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cCosto.Delete(idCosto);
            updateId(idCosto);
            updateGridView();
        }

        private void clean()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            
            button3.Enabled = false;
            button4.Enabled = false;
            if(sw==1)
            button1.Enabled = true;
       
        
        }

        private void updateId(int idProducto)
        {

            DataSet DS;
            DS = cCosto.Consultation2("select count(idCosto) from Costo");
            int count = int.Parse(DS.Tables[0].Rows[0][0].ToString()) + 1;

            if (idProducto != count)
                for (int i = idProducto; i <= count; i++)
                {

                    cCosto.SetId(i, i + 1);
                }
            else { }

        }

        private void getUltimate()
        {

            DataSet DS;
            DS = cCosto.Consultation2("select Max(idProducto),Sucursal_idSucursal from Producto");
            int max= int.Parse(DS.Tables[0].Rows[0][0].ToString());
            idSucursal = int.Parse(DS.Tables[0].Rows[0][1].ToString());
            textBox8.Text = "" + max;
         

        }


        private void button2_Click(object sender, EventArgs e)
        {
            clean();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cCosto.Set(textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, int.Parse(textBox8.Text));
            updateGridView();
        }

        private void Costos_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cCosto.Consultation("select *from Producto");
            dataGridView2.DataSource = cCosto.Consultation("select *from Costo");
            button3.Enabled = false;
            button4.Enabled = false;
            button1.Enabled = true;
            textBox8.Enabled = false;
            getUltimate();
            querySucursal();
            

          
        }

        private void querySucursal()
        {
            DataSet DS;
            DS = cCosto.Consultation2("select *from Sucursal where idSucursal='" + idSucursal + "'");
            arriendo= int.Parse(DS.Tables[0].Rows[0][6].ToString());

            DS = cCosto.Consultation2("select SUM(cantidadProducto) from Producto where Sucursal_idSucursal='" + idSucursal + "'");
            cantidadt= int.Parse(DS.Tables[0].Rows[0][0].ToString());
            DS = cCosto.Consultation2("select SUM(cantidadProducto) from Producto where Sucursal_idSucursal='" + idSucursal + "' and idProducto='" + idProducto + "'");
            cantidadp = int.Parse(DS.Tables[0].Rows[0][0].ToString());
            
            textBox3.Text =""+(( arriendo / cantidadt));
      
        
        }


        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idProducto = int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
            textBox8.Text = idProducto.ToString();
            sw = 1;
            button1.Enabled = true;
            DataSet DS;
            DS = cCosto.Consultation2("select *from Producto where idProducto='" + idProducto + "'");
            idSucursal=int.Parse(DS.Tables[0].Rows[0][9].ToString());
            querySucursal();
          
        }

        private void getdatos()
        {
            DataSet DS;
            DS = cCosto.Consultation2("select *from Costo where idCosto='" + idCosto + "'");
            textBox1.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox2.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox3.Text = DS.Tables[0].Rows[0][2].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][3].ToString();
            textBox5.Text = DS.Tables[0].Rows[0][4].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][6].ToString();
            textBox7.Text = DS.Tables[0].Rows[0][7].ToString();
            textBox8.Text = DS.Tables[0].Rows[0][8].ToString();
       

        }

        private void dataGridView2_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idCosto = int.Parse(dataGridView2[0, e.RowIndex].Value.ToString());
           // textBox8.Text = idCosto.ToString();
            button1.Enabled = false;
            button4.Enabled = true;
            button3.Enabled = true;
            getdatos();
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            string filtro = textBox9.Text;
            dataGridView1.DataSource = cCosto.Consultation("SELECT * FROM Producto WHERE nombreProducto LIKE '%" + filtro + "%' ");
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
